from twisted.application        import internet, service
from twisted.web                import server
from nevow                      import appserver
from APN.provider               import *
from APN.API                    import DeviceAPI, ProviderAPI
from APN.push                   import *
from APN.website                import MainPage

from twisted.internet import ssl, reactor
from twisted.internet.protocol import Factory, Protocol


# ===========
# = General =
# ===========
# Absolute or relative path to a file where information is stored
DATABASE_FILE = "yeeha.db"

# Flush Object to the database every n seconds (Recommended: 30)
FLUSH_INTERVAL = 60*60

# ============
# = AdminGUI =
# ============
# Port where the AdminGUI should listen on
ADMINGUI_PORT = 8443
ADMINGUI_CERT = "../cert/push.undsoversum.de.pyshD_cert.pem"
ADMINGUI_KEY =  "../cert/push.undsoversum.de.pyshD_key.pem"
ADMINGUI_CACERT = "../cert/pyshD-CA.pem"
ADMINGUI_LISTEN_ADDRESS =  "localhost"

# =============
# = DeviceAPI =
# =============
# TCP Port where the DeviceAPI listens. The DeviceAPI is used by iPhoneApps to register with the Provider
DEVICEAPI_PORT = 8081
DEVICEAPI_LISTEN_ADDRESS = "localhost"


# ===============
# = ProviderAPI =
# ===============
# Access to Provider methods from remote. Such as sendMessage. Can be used to integrate external tools
PROVIDERAPI_PORT = 8082
PROVIDERAPI_LISTEN_ADDRESS = "localhost"

# =================
# = Push Settings =
# =================

# Apple Push Servers (either gateway.sandbox.apple.com (TESTING) or gateway.push.apple.com)
APNS_SERVER_HOSTNAME = "gateway.sandbox.push.apple.com"
# Apple Push Servers Port (2195)
APNS_SERVER_PORT = 2195

# Simultanous Connections to Push Servers (recommended: 5 (not higher than 15))
APNS_SIMULTANOUS = 5

# Feedback Servers tell which devicetokens are outdated
FEEDBACK_SERVER_HOSTNAME = "feedback.sandbox.push.apple.com"
# Feedback Servers port (default: 2196)
FEEDBACK_SERVER_PORT = 2196
# Feedback Server check frequency in minutes (Not faster than 60 minutes! Don't stress the servers. It's unnecessary)
FEEDBACK_SERVER_CHECK_INTERVAL = 60

# Certificated for authentification vs. Apple Servers
#APNS_SSL_CERTIFICATE_FILE = "/Users/thorstenphilipp/Dropbox/Undsoversum iPhone App/pyshD/etc/certkey.pem"
#APNS_SSL_PRIVATE_KEY_FILE = "/Users/thorstenphilipp/Dropbox/Undsoversum iPhone App/pyshD/etc/pkey.pem"
APNS_SSL_CERTIFICATE_FILE = "../cert/de.undsoversum.test.push_cert.pem"
APNS_SSL_PRIVATE_KEY_FILE = "../cert/de.undsoversum.test.push_key.pem"



# ===================================
# = NO EDITING BELOW !!! Seriously! =
# ===================================

application = service.Application('pyshD')
serviceCollection =  service.IServiceCollection(application)

provider = Provider(DATABASE_FILE,FLUSH_INTERVAL)


i = 0
while(i < APNS_SIMULTANOUS):
    i += 1
    internet.SSLClient( APNS_SERVER_HOSTNAME,
                        APNS_SERVER_PORT,
                        APNSClientFactory(provider,instance=i),
                        APNSClientContextFactory(APNS_SSL_CERTIFICATE_FILE,APNS_SSL_PRIVATE_KEY_FILE)
                       ).setServiceParent(serviceCollection)

                   
internet.SSLClient( FEEDBACK_SERVER_HOSTNAME,
                   FEEDBACK_SERVER_PORT,
                   FEEDBACKClientFactory(provider,interval=FEEDBACK_SERVER_CHECK_INTERVAL),
                   APNSClientContextFactory(APNS_SSL_CERTIFICATE_FILE,APNS_SSL_PRIVATE_KEY_FILE)
                  ).setServiceParent(serviceCollection)
                   

internet.TCPServer(DEVICEAPI_PORT,server.Site(DeviceAPI(provider)),interface=DEVICEAPI_LISTEN_ADDRESS).setServiceParent(serviceCollection)
internet.TCPServer(PROVIDERAPI_PORT,server.Site(ProviderAPI(provider)),interface=PROVIDERAPI_LISTEN_ADDRESS).setServiceParent(serviceCollection)


internet.SSLServer(
                    ADMINGUI_PORT,
                    appserver.NevowSite(MainPage(provider)),
                    SSLServerContext(
                                        ADMINGUI_CERT,
                                        ADMINGUI_KEY,
                                        ADMINGUI_CACERT
                                    ),
                    interface=ADMINGUI_LISTEN_ADDRESS
                    
                   ).setServiceParent(serviceCollection)

